package com.example.park_management.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.park_management.common.PageParam;
import com.example.park_management.common.Result;
import com.example.park_management.domain.Complaints;
import com.example.park_management.domain.Expenses;
import com.example.park_management.service.ComplaintsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/complaints")
public class ComplaintsController {

    @Resource
    ComplaintsService complaintsService;

    @PostMapping("/")
    public Result add(@RequestBody Complaints complaints){
        try{
            boolean save = complaintsService.save(complaints);
            if (save){
                return Result.success();
            }
            return Result.fail();
        }catch (Exception e) {
            return Result.fail("输入格式异常！");
        }
    }

    @PutMapping("/")
    public Result update(@RequestBody Complaints complaints){
        try{
            boolean save = complaintsService.updateById(complaints);
            if (save){
                return Result.success();
            }
            return Result.fail();
        }catch (Exception e) {
            return Result.fail("输入格式异常！");
        }
    }

    @DeleteMapping("/{complaintId}")
    public Result delete(@PathVariable Integer complaintId){
        boolean b = complaintsService.removeById(complaintId);
        if (b){
            return Result.success();
        }
        return Result.fail();
    }

    @PostMapping("/page")
    public Result page(@RequestBody PageParam<Complaints> pageParam){
        Page<Complaints> complaintsPage = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());

        QueryWrapper<Complaints> complaintsQueryWrapper = new QueryWrapper<>();
        if (pageParam.getData() != null){
            if (pageParam.getData().getComplaintDescription() != null ){
                complaintsQueryWrapper.like("complaint_description", pageParam.getData().getComplaintDescription());
            }
            if (pageParam.getData().getStatus() != null && pageParam.getData().getStatus() != -1){
                complaintsQueryWrapper.eq("status", pageParam.getData().getStatus());
            }
            if (pageParam.getData().getOwnersId() != null && pageParam.getData().getOwnersId() != -1){
                complaintsQueryWrapper.eq("owners_id", pageParam.getData().getOwnersId());
            }
        }
        complaintsService.page(complaintsPage,complaintsQueryWrapper);

        return Result.success(complaintsPage.getRecords(),complaintsPage.getTotal());
    }

}
